package com.bw.controller;

import cn.hutool.jwt.JWT;
import com.bw.bean.Tuser;
import com.bw.service.TuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class TuserController {

    @Autowired
    private TuserService tuserService;

    @RequestMapping("login")
    @ResponseBody
    public Integer login(HttpServletResponse response, HttpSession session,String name, String password){
        Tuser tuser = tuserService.login(name,password);
        if (tuser != null){
            session.setAttribute("tuser",tuser);
            String sign = JWT.create().setPayload("id", tuser.getId()).setPayload("password", password).setKey("12345".getBytes()).sign();
            Cookie cookie = new Cookie("sign", sign);
            cookie.setDomain("localhost");
            cookie.setMaxAge(30*60);
            response.addCookie(cookie);
            return 1;
        }else {
            return 0;
        }
    }
}
